/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/11/7 12:22 上午
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.concurrency.juc.aqs;

import lombok.Data;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2019/11/7 12:22 上午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2019/11/7
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Data
public class ReentrantLockThreadForSignal implements ILockWaitWithCondition {

    private Condition condition;
    private ReentrantLock reentrantLock;

    @Override
    public void run(){
        while (true) {
            System.out.println("ReentrantLockThreadForSignal start lock");
            try {
                reentrantLock.lock();
                System.out.println("begin signal");
                condition.signal();
                System.out.println("end signal");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
                System.out.println("ReentrantLockThreadForSignal end lock");
            }

            try {
                System.out.println("ReentrantLockThreadForSignal sleeping");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("ReentrantLockThreadForSignal---------------------------");
        }
    }

    @Override
    public void condition(Condition condition, ReentrantLock reentrantLock) {
        setCondition(condition);
        setReentrantLock(reentrantLock);
    }
}

